<?php
// +----------------------------------------------------------------------
// | Azthink通用开发平台 [ TOMORROW IS ANOTHER DAY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2019 http://www.azthink.top All rights reserved.
// +----------------------------------------------------------------------
// | Author 李忠<azthink@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\model;

use think\Model;

/**
 * 行为日志模型
 * @author 李忠<azthink@qq.com>
 * @createAt 2018-09-02 15:32:30
 */
class Log extends Model {
    // 设置当前模型对应的完整数据表名称
    protected $table = '__SYS_LOG__';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    //数据自动完成
    protected $auto = ['create_by' => UID];

    //修改器
    public function getStatusTextAttr($value, $data) {
        return get_status_html($data['status']);
    }

    public function getModuleTextAttr($value, $data) {
        return get_config_text('module_list', $data['module']);
    }

    public function getActionTextAttr($value, $data) {
        return get_config_text('action_list', $data['action']);
    }


    //自定义方法
    public static function lists($module, $model, $action, $record_id, $remark) {
        $map = array();

        array_push($map, ['a.status', '=', 1]);

        if ($module !== '' && !empty($module)) {
            array_push($map, ['a.module', '=', $module]);
        }
        if ($model !== '' && !empty($model)) {
            array_push($map, ['a.model', '=', $model]);
        }
        if ($action !== '' && !empty($action)) {
            array_push($map, ['a.action', '=', $action]);
        }
        if ($record_id !== '' && !empty($record_id)) {
            array_push($map, ['a.record_id', '=', $record_id]);
        }

        if ($remark !== '' && !empty($remark)) {
            array_push($map, ['a.remark', 'like', '%' . $remark . '%']);
        }
        return Log::alias('a')->join('__SYS_MEMBER__ b', 'a.create_by = b.id')->where($map)
            ->field('a.*, b.title as create_by_cn')->order('a.id desc')->paginate();
    }

    public static function state($id, $status) {
        $data['id'] = $id;
        $data['status'] = $status;
        return Log::update($data);
    }
}
